package model.dao;

import java.util.List;

import model.entity.Participante;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * Participante DAO
 *
 * @author Alex
 *
 */

@Repository(value="participanteDAO")
public class ParticipanteDAO implements IParticipanteDAO {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 *
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 *
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Add Participante
	 *
	 * @param Participante
	 *            participante
	 */
	@Override
	public void addParticipante(Participante participante) {
		getSessionFactory().getCurrentSession().save(participante);
	}

	/**
	 * Delete Participante
	 *
	 * @param Participante
	 *            participante
	 */
	@Override
	public void deleteParticipante(Participante participante) {
		getSessionFactory().getCurrentSession().delete(participante);
	}

	/**
	 * Update Participante
	 *
	 * @param Participante
	 *            participante
	 */
	@Override
	public void updateParticipante(Participante participante) {
		getSessionFactory().getCurrentSession().update(participante);
	}

	/**
	 * Get Participante
	 *
	 * @param int Participante email
	 * @return Participante
	 */
	@Override
	public Participante getParticipanteByEmail(String email) {
		@SuppressWarnings("unchecked")
		List<Participante> list = getSessionFactory().getCurrentSession()
				.createQuery("from Participante where partEmail=?")
				.setParameter(0, email).list();
		return (list.isEmpty()) ? null : list.get(0);
	}

	/**
	 * Get Participante List
	 *
	 * @return List - Participante list
	 */
	@Override
	public List<Participante> getParticipantes() {
		@SuppressWarnings("unchecked")
		List<Participante> list = getSessionFactory().getCurrentSession()
				.createQuery("from Participante").list();
		return list;
	}

}
